/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FenNouvPartie.java
 *
 * Created on 21 déc. 2011, 17:51:12
 */
package InterfaceGraphique;

import common.Constantes;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import org.jdesktop.application.Action;
import plateau.Joueur;

/**
 *
 * @author Arthur
 */
public class FenNouvPartie extends javax.swing.JFrame {

     private ImagePanel FondFenNouvPartie;

    /** Creates new form FenNouvPartie */
    public FenNouvPartie() {

        
        ImageIcon image3 = new ImageIcon(getClass().getResource("/images/fondBOIS2.jpg"));
        ImageIcon imageRedim3 = new ImageIcon(image3.getImage().getScaledInstance(1024, 768, Image.SCALE_DEFAULT));
        Image imageFond2 = imageRedim3.getImage();

       FondFenNouvPartie = new ImagePanel(imageFond2);
       MainApplication.DefinirFondFenetre(this, imageFond2, FondFenNouvPartie);       
       
       initComponents();

        this.jPanel1.setVisible(false);
        this.getContentPane().setBackground(new Color(237,200,132));
        this.jPanel2.setBackground(new java.awt.Color(110,48,34));
        
        try {
            System.out.println(System.getProperties().get("user.dir"));
            Font font = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("/polices/cartoon.ttf"));
            font = font.deriveFont(18.0f);
            this.jLabel1.setFont(font);
            this.jLabel1.setForeground(new java.awt.Color(213, 151, 0));
        } catch (Exception ex) {
            //Logger.getLogger(jPanel2.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("erreur police fenêtre d'accueil");
        }
        
        this.listePanJoueur.add(this.panAddJoueur1);
        this.panAddJoueur1.setBackground(new Color(237,200,132));
        this.listePanJoueur.add(this.panAddJoueur2);
        this.panAddJoueur2.setBackground(new Color(237,200,132));
        this.listePanJoueur.add(this.panAddJoueur3);
        this.panAddJoueur3.setBackground(new Color(237,200,132));
        this.listePanJoueur.add(this.panAddJoueur4);
        this.panAddJoueur4.setBackground(new Color(237,200,132));
        this.listePanJoueur.add(this.panAddJoueur5);
        this.panAddJoueur5.setBackground(new Color(237,200,132));
        this.listePanJoueur.add(this.panAddJoueur6);
        this.panAddJoueur6.setBackground(new Color(237,200,132));

   
      
 
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lInfos = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        panAddJoueur5 = new InterfaceGraphique.PanAddJoueur();
        panAddJoueur4 = new InterfaceGraphique.PanAddJoueur();
        panAddJoueur3 = new InterfaceGraphique.PanAddJoueur();
        panAddJoueur6 = new InterfaceGraphique.PanAddJoueur();
        panAddJoueur1 = new InterfaceGraphique.PanAddJoueur();
        panAddJoueur2 = new InterfaceGraphique.PanAddJoueur();
        jLabel1 = new javax.swing.JLabel();
        cbNbJoueurs = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(0, 500));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setName("jPanel1"); // NOI18N

        lInfos.setForeground(new java.awt.Color(153, 0, 0));
        lInfos.setText(" ");
        lInfos.setName("lInfos"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lInfos, javax.swing.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(lInfos, javax.swing.GroupLayout.DEFAULT_SIZE, 14, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setMinimumSize(new java.awt.Dimension(365, 497));
        jPanel2.setName("jPanel2"); // NOI18N

        panAddJoueur5.setName("panAddJoueur5"); // NOI18N
        panAddJoueur5.hide();

        panAddJoueur4.setName("panAddJoueur4"); // NOI18N
        panAddJoueur4.hide();

        panAddJoueur3.setName("panAddJoueur3"); // NOI18N

        panAddJoueur6.setName("panAddJoueur6"); // NOI18N
        panAddJoueur6.hide();

        panAddJoueur1.setName("panAddJoueur1"); // NOI18N

        panAddJoueur2.setName("panAddJoueur2"); // NOI18N

        jLabel1.setText("Nombre de joueurs :");
        jLabel1.setName("jLabel1"); // NOI18N

        cbNbJoueurs.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "3", "4", "5", "6" }));
        cbNbJoueurs.setName("cbNbJoueurs"); // NOI18N
        cbNbJoueurs.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbNbJoueursItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panAddJoueur2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(panAddJoueur3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(panAddJoueur4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(panAddJoueur5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(175, 175, 175)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbNbJoueurs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(panAddJoueur1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(panAddJoueur6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(76, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cbNbJoueurs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panAddJoueur1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panAddJoueur2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panAddJoueur3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panAddJoueur4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panAddJoueur5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panAddJoueur6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34))
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(FenNouvPartie.class, this);
        jButton1.setAction(actionMap.get("create")); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/entrerjeu.png"))); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/retour.png"))); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jMenuBar1.setName("jMenuBar1"); // NOI18N

        jMenu1.setText("File");
        jMenu1.setName("jMenu1"); // NOI18N
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenu2.setName("jMenu2"); // NOI18N
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 181, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButton1, jButton2});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 365, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jButton1, jButton2});

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-552)/2, (screenSize.height-506)/2, 552, 506);
    }// </editor-fold>//GEN-END:initComponents

    private void cbNbJoueursItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbNbJoueursItemStateChanged
        // TODO add your handling code here:
        int nbJ = new Integer((String) evt.getItem());
        int i;
        for ( i = 3 ; i < nbJ ; ++i ) {
            this.listePanJoueur.get(i).setVisible(true);
        }
        for( ; i < 6 ; ++i ) {
            this.listePanJoueur.get(i).setVisible(false);
        }
    }//GEN-LAST:event_cbNbJoueursItemStateChanged

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
          MainApplication.hideAll();
          MainApplication.showFenAccueil();
    }//GEN-LAST:event_jButton2MouseClicked

    /**
     * @param args the command line arguments
     */
    

public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FenNouvPartie.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FenNouvPartie.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FenNouvPartie.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FenNouvPartie.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FenNouvPartie().setVisible(true);
            }
        });
    }



@Action
/*
 * Lancement de la partie
 * @author : Arthur
 */
public void create() {
    // Création de la liste des joueurs
    ArrayList<Joueur> listeJoueur = new ArrayList<Joueur>();
    
    // Récupération du nombre de joueur choisi
    int nbJ = this.cbNbJoueurs.getSelectedIndex() + 3;
    
    // Pour chaque joueur
    for ( int i = 0 ; i < nbJ ; ++i ) {
        // On vérifie que les infos ont été entrée correctement
        if( this.listePanJoueur.get(i).getjTextField1().getText() != null
                && this.listePanJoueur.get(i).getButtonGroup1().getSelection() != null) {
            int sexe = -1;
            // On parcours les JComboBox du ButtonGroup pour tester celui qui a été choisi (le isSelected)
            Enumeration elements = this.listePanJoueur.get(i).getButtonGroup1().getElements();
            while (elements.hasMoreElements()) {
                AbstractButton button = (AbstractButton)elements.nextElement();
                // Association du texte "Homme/Femme" avec la constante du programme
                if (button.isSelected()) {
                    sexe = ( button.getText().equals( "Homme" )) ? Constantes.SEX_MAS : Constantes.SEX_FEM;
                }
            }  
            if( this.listePanJoueur.get(i).getjTextField1().getText().length() < 20 ) {
                listeJoueur.add( new Joueur(this.listePanJoueur.get(i).getjTextField1().getText(), sexe));
            }
            else {
                new BoiteMessage("Erreur : Nom du joueur n° "+ (i+1) +" trop long." );
                return;
            }   
        }
        else {
            new BoiteMessage("Erreur : Joueurs non remplis " );
            return;
        }
    }
    MainApplication.lancerPartie(listeJoueur);
}
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbNbJoueurs;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lInfos;
    private InterfaceGraphique.PanAddJoueur panAddJoueur1;
    private InterfaceGraphique.PanAddJoueur panAddJoueur2;
    private InterfaceGraphique.PanAddJoueur panAddJoueur3;
    private InterfaceGraphique.PanAddJoueur panAddJoueur4;
    private InterfaceGraphique.PanAddJoueur panAddJoueur5;
    private InterfaceGraphique.PanAddJoueur panAddJoueur6;
    // End of variables declaration//GEN-END:variables
    private ArrayList<PanAddJoueur> listePanJoueur = new ArrayList<PanAddJoueur> ();
}
